package com.newgrand.utils;


import lombok.extern.slf4j.Slf4j;
import lombok.var;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
public class GetPhIdHelper {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    public static String GetSQLStringBy(List<String> list, String inStr) {
        String sql = GetSQL(list, "", inStr);
        if (!sql.isEmpty()) {
            sql = "  ( " + sql.substring(0, sql.length() - 2) + " ) ";
        }
        return sql;
    }

    private static String GetSQL(List<String> list, String sql, String inStr) {
        if (list.size() > 1000) {
            var listNew = list.subList(0, 998);
            sql += inStr + " ('" + StringUtils.join(listNew, "','") + "')  or";
            sql = GetSQL(list.subList(999, list.size()), sql, inStr);
        } else {
            sql += inStr + "  ('" + StringUtils.join(list, "','") + "')  or";
        }
        return sql;
    }

    public static String StringForO(Object data) {
        return StringUtils.isEmpty(String.valueOf(data)) ? "0" : "null".equals(String.valueOf(data)) ? "0" : String.valueOf(data);
    }

    /**
     * 得到 全拼
     *
     * @param src
     * @return
     */
    public static String getPingYin(String src) {
        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else {
                    t4 += Character.toString(t1[i]);
                }
            }
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4;
    }

    /**
     * 得到中文首字母
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {
        StringBuilder convert = new StringBuilder();
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            } else {
                convert.append(word);
            }
        }
        return convert.toString();
    }

    /**
     * 将字符串转移为ASCII码
     *
     * @param cnStr
     * @return
     */
    public static String getCnASCII(String cnStr) {
        StringBuilder strBuf = new StringBuilder();
        byte[] bGBK = cnStr.getBytes();
        for (byte b : bGBK) {
            strBuf.append(Integer.toHexString(b & 0xff));
        }
        return strBuf.toString();
    }

    /*
     * 功能描述: 通过编码获取表主键
     * @Param: [tname, codeName, codeValue]
     * @Return: java.lang.String
     */
    public String GetPhIdByCode(String tname, String codeName, String codeValue) {
        String r = "";
        String sql = "select phid from " + tname + " where  " + codeName + "='" + codeValue + "'";
        List<String> list = jdbcTemplate.queryForList(sql, String.class);
        if (!list.isEmpty()) r = list.get(0);
        return r;
    }

    /*
     * 功能描述: 通过编码获取表主键
     * @Param: [tname, codeName, codeValue]
     * @Return: java.lang.String
     */
    public String GetPhIdByCode(String tname, String codeName, String codeValue, String filter) {
        String r = "";
        String sql = "select phid from " + tname + " where  " + codeName + "= '" + codeValue + "' and " + filter;
        List<String> list = jdbcTemplate.queryForList(sql, String.class);
        if (!list.isEmpty()) r = list.get(0);
        return r;
    }

    /*
     * 功能描述: 通过编码获取表主键
     * @Param: [tname, codeName, codeValue]
     * @Return: java.lang.String
     */
    public String GetPhIdByFilter(String tname, String filter) {
        String r = "";
        String sql = "select phid from " + tname + " where " + filter;
        List<String> list = jdbcTemplate.queryForList(sql, String.class);
        if (!list.isEmpty()) {
            r = list.get(0);
        }
        return r;
    }

    /*
     * 功能描述: 通过编码获取表对应字段
     * @Param: [tName, codeName, cnoName,cnoValue]
     * @Return: java.lang.String
     */
    public String GetCodeByCode(String tName, String codeName, String cnoName, String cnoValue) {
        String r = "";
        String sql = "select " + codeName + " from " + tName + " where " + cnoName + "  ='" + cnoValue + "'";
        List<String> list = jdbcTemplate.queryForList(sql, String.class);
        if (!list.isEmpty()) r = list.get(0);
        return r;
    }

    /*
     * 功能描述: 通过编码获取表对应字段
     * @Param: [tName, codeName, cnoName,cnoValue]
     * @Return: java.lang.String
     */
    public String GetCodeByCode(String tName, String codeName, String cnoName, String cnoValue, String filter) {
        String r = "";
        String sql = "select " + codeName + " from " + tName + " where " + cnoName + "  ='" + cnoValue + "'";
        if (StringUtils.isEmpty(filter)) {
            sql += " and " + filter;
        }
        List<String> list = jdbcTemplate.queryForList(sql, String.class);
        if (!list.isEmpty()) r = list.get(0);
        return r;
    }

    /*
     * 功能描述: 通过过滤添加获取表对应字段
     * @Param: [tName, codeName, cnoName,cnoValue]
     * @Return: java.lang.String
     */
    public String GetCodeByFilter(String tName, String codeName, String filter) {
        String r = "";
        String sql = "select " + codeName + " from " + tName + " where " + filter;
        List<String> list = jdbcTemplate.queryForList(sql, String.class);
        if (!list.isEmpty()) r = list.get(0);
        return r;
    }

    /**
     * 获取phid
     *
     * @param request
     * @return
     */
    public String getPhId(HttpServletRequest request) {
        System.out.println("接口调用成功！！！！！！");
        String PhId = "";
        PhId = request.getParameter("Phid");//唯一标识主键
        if (StringUtils.isEmpty(PhId)) {
            PhId = request.getParameter("phid");//唯一标识主键
            if (StringUtils.isEmpty(PhId)) {
                PhId = request.getParameter("PHID");//唯一标识主键
            }
        }
        System.out.println("PhId = " + PhId);
        PhId = PhId.replace("\"", "");
        return PhId;
    }
}
